package com.hxd.otc.module.my.presenter;

import android.support.annotation.NonNull;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.module.my.bean.ExchangeRecordBean;
import com.hxd.otc.module.my.bean.ExchangeRuleBean;
import com.hxd.otc.module.my.contract.ExchangeContract;
import com.hxd.otc.module.my.model.ExchangeModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.utils.ResourceUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeMap;

/**
 * Created by lzy on 2018/12/26 0026.
 * description ${兑换Presenter}
 */
public class ExchangePresenter extends BasePresenter<ExchangeContract.IView> implements ExchangeContract.IPresenter {

    private final ExchangeModel mExchangeModel;

    private TreeMap<String, ArrayList<String>> mSymbolMaps;
    private HashMap<String, ExchangeRuleBean> mExchangeRuleMaps;
    private HashMap<String, String> mExchangeTaxMaps = new HashMap<>();


    public ExchangePresenter(ExchangeContract.IView iView) {
        super(iView);
        mExchangeModel = ExchangeModel.getInstance();
    }

    @Override
    public void exchangeCoinRule() {
        mExchangeModel.exchangeCoinRule(new BaseObserver<ArrayList<ExchangeRuleBean>>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull ArrayList<ExchangeRuleBean> exchangeRuleBeans) {
                mSymbolMaps = new TreeMap<>();
                mExchangeRuleMaps = new HashMap<>();

                for (ExchangeRuleBean exchangeRuleBean : exchangeRuleBeans) {
                    String outSymbol = exchangeRuleBean.getOutSymbol();
                    String inSymbol = exchangeRuleBean.getInSymbol();
                    if (mSymbolMaps.containsKey(outSymbol)) {
                        mSymbolMaps.get(outSymbol).add(inSymbol);
                    } else {
                        ArrayList<String> inSymbols = new ArrayList<>();
                        inSymbols.add(inSymbol);
                        mSymbolMaps.put(outSymbol, inSymbols);
                    }
                    mExchangeRuleMaps.put(getPair(outSymbol, inSymbol), exchangeRuleBean);
                }
                mView.getExchangeCoinRuleSuccess();
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
            }
        }, mView.<BaseBean<ArrayList<ExchangeRuleBean>>>bindAutoDispose());
    }

    private TreeMap<String, ArrayList<String>> getSymbolMaps() {
        if (mSymbolMaps == null) {
            mView.showToast(ResourceUtil.getString(R.string.get_exchange_info_fail_and_retry_get));
            exchangeCoinRule();
        }
        return mSymbolMaps;
    }

    @Override
    public ArrayList<String> getOutSymbols() {
        ArrayList<String> outSymbols = new ArrayList<>();
        TreeMap<String, ArrayList<String>> symbolMaps = getSymbolMaps();
        if (symbolMaps != null) {
            outSymbols.addAll(symbolMaps.keySet());
        }
        return outSymbols;
    }

    @Override
    public void requestExchangeTax(final String outSymbol, final String inSymbol) {
        String pair = getPair(outSymbol, inSymbol);

        if (mExchangeTaxMaps.containsKey(pair)) {
            mView.getExchangeTaxSuccess(outSymbol, inSymbol, mExchangeTaxMaps.get(pair));
        }

        mExchangeModel.exchangeTax(outSymbol, inSymbol, new BaseObserver<String>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull String taxValue) {
                mExchangeTaxMaps.put(getPair(outSymbol, inSymbol), taxValue);
                mView.getExchangeTaxSuccess(outSymbol, inSymbol, taxValue);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
            }
        }, mView.<BaseBean<String>>bindAutoDispose());
    }

    @Override
    public ArrayList<String> getInSymbols(String outSymbol) {
        TreeMap<String, ArrayList<String>> symbolMaps = getSymbolMaps();
        if (symbolMaps != null && symbolMaps.containsKey(outSymbol)) {
            return symbolMaps.get(outSymbol);
        } else {
            return new ArrayList<>();
        }
    }

    private HashMap<String, ExchangeRuleBean> getExchangeRuleMaps() {
        if (mExchangeRuleMaps == null) {
            mView.showToast(ResourceUtil.getString(R.string.get_exchange_info_fail_and_retry_get));
            exchangeCoinRule();
        }
        return mExchangeRuleMaps;
    }

    @Override
    public ExchangeRuleBean getExchangeRuleBeanByPair(String pair) {
        if (getExchangeRuleMaps() == null || mExchangeRuleMaps.size() == 0 || !mExchangeRuleMaps.containsKey(pair)) return null;
        return mExchangeRuleMaps.get(pair);
    }

    @Override
    public String getTaxByPair(String outSymbol, String inSymbol, boolean isReGetTax) {
        String pair = getPair(outSymbol, inSymbol);
        if (!mExchangeTaxMaps.containsKey(pair)) {
            requestExchangeTax(outSymbol, inSymbol);
            return "";
        }
        return mExchangeTaxMaps.get(pair);
    }

    @Override
    public void exchangePay(String outSymbol, String inSymbol, String outVolume) {
        mExchangeModel.exchangePay(outSymbol, inSymbol, outVolume, new BaseObserver<ExchangeRecordBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull ExchangeRecordBean exchangeRecordBean) {
                mView.exchangePaySuccess(exchangeRecordBean);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.exchangePayFail(errMessage);
            }
        }, mView.<BaseBean<ExchangeRecordBean>>bindAutoDispose());
    }

    @Override
    public String getPair(String outSymbol, String inSymbol) {
        return outSymbol + "_" + inSymbol;
    }

}
